/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fri.cbw.core;

import fri.cbw.ToolGraph.ToolGraphSceneImpl;
import fri.cbw.ToolPalette.ToolTypeNodeFactory;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import org.netbeans.api.settings.ConvertAsProperties;
import org.netbeans.spi.palette.PaletteActions;
import org.netbeans.spi.palette.PaletteController;
import org.netbeans.spi.palette.PaletteFactory;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.Lookup;
import org.openide.util.NbBundle.Messages;
import org.openide.util.lookup.Lookups;
import org.openide.windows.TopComponent;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
    dtd = "-//fri.cbw.core//Core//EN",
autostore = false)
@TopComponent.Description(
    preferredID = "CoreTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(position=1, mode = "editor", openAtStartup = true)
@ActionID(category = "Window", id = "fri.cbw.core.CoreTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
    displayName = "#CTL_CoreAction",
preferredID = "CoreTopComponent")
@Messages({
    "CTL_CoreAction=Core",
    "CTL_CoreTopComponent=Core Window",
    "HINT_CoreTopComponent=This is a Core window"
})
public final class CoreTopComponent extends TopComponent {
    
    //private ToolTransferHandler th = new ToolTransferHandler();
    ToolGraphSceneImpl scene;
    
    public CoreTopComponent() {
        initComponents();
        setName(Bundle.CTL_CoreTopComponent());
        setToolTipText(Bundle.HINT_CoreTopComponent());
        
        
        Node root = new AbstractNode(Children.create(new ToolTypeNodeFactory(), false));
        PaletteActions a = new MyPaletteActions();
        PaletteController p = PaletteFactory.createPalette(root, a);
        associateLookup(Lookups.fixed(p));
        
        
        scene = new ToolGraphSceneImpl();
        JComponent view = scene.createView();
        scenePanel.setViewportView(view);
        add(scene.createSatelliteView(), BorderLayout.EAST);
        
        this.repaint();
    }
    
    private static final class MyPaletteActions  extends PaletteActions {
      
      @Override
      public Action[] getImportActions() {
         return new Action[]{};
      }
      
      @Override
      public Action[] getCustomPaletteActions() {
         return new Action[]{};
      }
      
      @Override
      public Action[] getCustomCategoryActions(Lookup arg0) {
         return new Action[]{};
      }
      
      @Override
      public Action[] getCustomItemActions(Lookup arg0) {
         return new Action[]{};
      }
      
      @Override
      public Action getPreferredAction(Lookup arg0) {
         return new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
               throw new UnsupportedOperationException("Not supported yet.");
            }
         };
      }
   }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT
     * modify this code. The content of this method is always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButtonSave = new javax.swing.JButton();
        jButtonLoad = new javax.swing.JButton();
        jButtonClear = new javax.swing.JButton();
        scenePanel = new javax.swing.JScrollPane();

        setLayout(new java.awt.BorderLayout());

        org.openide.awt.Mnemonics.setLocalizedText(jButtonSave, org.openide.util.NbBundle.getMessage(CoreTopComponent.class, "CoreTopComponent.jButtonSave.text")); // NOI18N
        jButtonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveActionPerformed(evt);
            }
        });
        jPanel1.add(jButtonSave);

        org.openide.awt.Mnemonics.setLocalizedText(jButtonLoad, org.openide.util.NbBundle.getMessage(CoreTopComponent.class, "CoreTopComponent.jButtonLoad.text")); // NOI18N
        jButtonLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLoadActionPerformed(evt);
            }
        });
        jPanel1.add(jButtonLoad);

        org.openide.awt.Mnemonics.setLocalizedText(jButtonClear, org.openide.util.NbBundle.getMessage(CoreTopComponent.class, "CoreTopComponent.jButtonClear.text")); // NOI18N
        jButtonClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonClearActionPerformed(evt);
            }
        });
        jPanel1.add(jButtonClear);

        add(jPanel1, java.awt.BorderLayout.PAGE_END);
        add(scenePanel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveActionPerformed
        scene.save();
    }//GEN-LAST:event_jButtonSaveActionPerformed

    private void jButtonLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLoadActionPerformed
        scene.load();
    }//GEN-LAST:event_jButtonLoadActionPerformed

    private void jButtonClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonClearActionPerformed
        scene.clear();
    }//GEN-LAST:event_jButtonClearActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonClear;
    private javax.swing.JButton jButtonLoad;
    private javax.swing.JButton jButtonSave;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane scenePanel;
    // End of variables declaration//GEN-END:variables
    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }
}
